# ============================================================
# Thomas König & Stefan Eschenwecker
# The European Court of Justice and legal European integration
# 
# This script produces Table 1 in the main text, 
# and Tables A2, A3, and A4 in the online appendix.
# ============================================================

# load packages
library(rstan)
library(brms)
library(modelsummary)
library(future.apply)
library(tidyverse)

# specify options for parallel computation
plan(multisession, workers = 8)
options("modelsummary_future" = T)
options("modelsummary_format_numeric_latex" = "plain")

# load models
EqualVar <- readRDS("Court/Models/EqualPrecision.rds")
UnequalVarMSNet <- readRDS("Court/Models/UnequalPrecision.rds")

# regression Table 1 (main text)

# order variables and name parameters to show
TableParams <- c(
  "b_supra_signalStrongPS" = "EU Both PS",
  "b_supra_signalWeakPS" = "EU Single PS",
  "b_supra_signalCont" = "EU Contradictory",
  "b_supra_signalWeakME" = "EU Single ME",
  "b_supra_signalStrongME" = "EU Both ME",
  "b_net_ms_pref_std" = "Weighted MS Net Preferences",
  "b_share_judges_std" = "Share Judges Loc",
  "sd_iuropa_case_id__Intercept" = "SD Cases Loc",
  "bsp_disc_mosupra_signal_int" = "EU Strength (Monotonic)",
  "b_disc_ms_signal_int_std" = "MS Strength",
  "b_disc_share_judges_std" = "Share Judges Disp",
  "sd_iuropa_case_id__disc_Intercept" = "SD Cases Disp"
)

# goodness of fit statistics
GOFData <- tribble(
  ~raw, ~clean, ~fmt,
  "nobs", "N Legal Issues", 0,
  "looic", "LOOIC", 1,
  "looic.se", "LOOIC s.e", 1,
)

# model names
ModelNames <- list(
  "Direction-Only Model" = EqualVar,
  "Combined Model" = UnequalVarMSNet
)

# generate Latex output
# (add N Cases from summary output manually +
# simplex parameters + 
# minor formatting like order and font types)
# (Hint: takes some time to run)
modelsummary(ModelNames,
             output = "latex", fmt = fmt_decimal(2),
             statistic = "conf.int", conf_level = .95,
             centrality = "mean",
             coef_map = TableParams,
             gof_map = GOFData,
             title = "Summary results of Bayesian hierarchical ordinal probit models."
)



# regression Table A2 (robustness check appendix)

# load models
FullData <- readRDS("Court/Models/UnequalPrecision_FullData.rds")
NoHSAG <- readRDS("Court/Models/UnequalPrecision_NoHSAG.rds")

# order variables and name parameters to show
TableParams <- c(
  "b_supra_signalStrongPS" = "EU Both PS",
  "b_supra_signalWeakPS" = "EU Single PS",
  "b_supra_signalCont" = "EU Contradictory",
  "b_supra_signalWeakME" = "EU Single ME",
  "b_supra_signalStrongME" = "EU Both ME",
  "b_net_ms_pref_std" = "Weighted MS Net Preferences",
  "b_share_judges_std" = "Share Judges Loc",
  "sd_iuropa_case_id__Intercept" = "SD Cases Loc",
  "bsp_disc_mosupra_signal_int" = "EU Strength (Monotonic)",
  "b_disc_ms_signal_int_std" = "MS Strength",
  "b_disc_share_judges_std" = "Share Judges Disp",
  "sd_iuropa_case_id__disc_Intercept" = "SD Cases Disp"
)

# goodness of fit statistics
GOFData <- tribble(
  ~raw, ~clean, ~fmt,
  "nobs", "N Legal Issues", 0,
  "looic", "LOOIC", 1,
  "looic.se", "LOOIC s.e", 1,
)

# model names
ModelNames <- list(
  "No HS AG" = NoHSAG,
  "Full Data" = FullData
)

# generate Latex output
# (add N Cases from summary output manually +
# simplex parameters + 
# minor formatting like order and font types)
# (Hint: takes some time to run)
modelsummary(ModelNames,
             output = "latex", fmt = fmt_decimal(2),
             statistic = "conf.int", conf_level = .95,
             centrality = "mean",
             coef_map = TableParams,
             gof_map = GOFData,
             title = "Models with different sample specifications."
)


# regression Table A3 (robustness check appendix)

# load models
SumMS <- readRDS("Court/Models/UnequalPrecision_SumMSObs.rds")
NoMS <- readRDS("Court/Models/UnequalPrecision_NoMS.rds")

# order variables and name parameters to show
TableParams <- c(
  "b_supra_signalStrongPS" = "EU Both PS",
  "b_supra_signalWeakPS" = "EU Single PS",
  "b_supra_signalCont" = "EU Contradictory",
  "b_supra_signalWeakME" = "EU Single ME",
  "b_supra_signalStrongME" = "EU Both ME",
  "b_sum_ms_ps_std" = "Sum MS PS",
  "b_sum_ms_me_std" = "Sum MS ME",
  "b_share_judges_std" = "Share Judges Loc",
  "sd_iuropa_case_id__Intercept" = "SD Cases Loc",
  "bsp_disc_mosupra_signal_int" = "EU Strength (Monotonic)",
  "b_disc_ms_signal_int_std" = "MS Strength",
  "b_disc_share_judges_std" = "Share Judges Disp",
  "sd_iuropa_case_id__disc_Intercept" = "SD Cases Disp"
)


# goodness of fit statistics
GOFData <- tribble(
  ~raw, ~clean, ~fmt,
  "nobs", "N Legal Issues", 0,
  "looic", "LOOIC", 1,
  "looic.se", "LOOIC s.e", 1,
)

# model names
ModelNames <- list(
  "Sum MS Pos" = SumMS,
  "No MS" = NoMS
)

# generate Latex output
# (add N Cases from summary output manually +
# simplex parameters + 
# minor formatting like order and font types)
# (Hint: takes some time to run)
modelsummary(ModelNames,
             output = "latex", fmt = fmt_decimal(2),
             statistic = "conf.int", conf_level = .95,
             centrality = "mean",
             coef_map = TableParams,
             gof_map = GOFData,
             title = "{Models with alternative operationalization and omitting member state observations."
)


# regression Table A4 (robustness check appendix)

# load model
CaseCharacteristics <- readRDS("Court/Models/UnequalPrecision_CaseCovariates.rds")

# order variables and name parameters to show
TableParams <- c(
  "b_supra_signalStrongPS" = "EU Both PS",
  "b_supra_signalWeakPS" = "EU Single PS",
  "b_supra_signalCont" = "EU Contradictory",
  "b_supra_signalWeakME" = "EU Single ME",
  "b_supra_signalStrongME" = "EU Both ME",
  "b_net_ms_pref_std" = "Weighted MS Net Preferences",
  "b_share_judges_std" = "Share Judges Loc",
  "b_about_derogationyes" = "About Derogation Loc",
  "b_about_validityyes" = "About Validity Loc",
  "b_about_direct_effectyes" = "About Direct Effect Loc",
  "b_count_subject_keywords" = "N Subject Keywords Loc",
  "sd_iuropa_case_id__Intercept" = "SD Cases Loc",
  "sd_year_lodged__Intercept" = "SD Year Lodged Loc",
  "bsp_disc_mosupra_signal_int" = "EU Strength (Monotonic)",
  "b_disc_ms_signal_int_std" = "MS Strength",
  "b_disc_share_judges_std" = "Share Judges Disp",
  "b_disc_about_derogationyes" = "About Derogation Disp",
  "b_disc_about_validityyes" = "About Validity Disp",
  "b_disc_about_direct_effectyes" = "About Direct Effect Disp",
  "b_disc_count_subject_keywords" = "N Subject Keywords Disp",
  "sd_iuropa_case_id__disc_Intercept" = "SD Cases Disp",
  "sd_year_lodged__disc_Intercept" = "SD Year Lodged Disp"
)


# goodness of fit statistics
GOFData <- tribble(
  ~raw, ~clean, ~fmt,
  "nobs", "N Legal Issues", 0,
  "looic", "LOOIC", 1,
  "looic.se", "LOOIC s.e", 1,
)


# generate Latex output
# (add N Cases + N Years from summary output manually)
# (Hint: takes some time to run)
modelsummary(CaseCharacteristics,
             output = "latex", fmt = fmt_decimal(2),
             statistic = "conf.int", conf_level = .95,
             centrality = "mean",
             coef_map = TableParams,
             gof_map = GOFData,
             title = "Combined Model incorporating additional case characteristics."
)
